<template>
    <div class="mainDiv">
        <el-table :data="list" :stripe="true" height="100%" @row-click="selecRow" @row-dblclick="submit">
            <el-table-column prop="gradeid" label="权限等级"></el-table-column>
            <el-table-column prop="remarks" label="等级描述"></el-table-column>
            <el-table-column prop="edittime" label="修改时间">
                <template slot-scope="scope">
                    {{time(scope.row.edittime)}}
                </template>
            </el-table-column>
            <el-table-column label="选择">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.selected" on-color="#13ce66" off-color="#ff4949" @change="selegrade(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
        </el-table>
        <div class="rowHead">
            <el-button style="float: right; margin-left: 5px" size="mini" type="success" icon="check" @click="submit">确定选择</el-button>
            <el-button style="float: right" size="mini" type="danger" icon="close" @click="close">取消关闭</el-button>
        </div>
    </div>
</template>
<script>
import C from '@/common/common.js'

export default {
  name: 'selectGrade',
  data () {
    return {
      list: [],
      selid: 0,
      id: 0
    }
  },
  methods: {
    selegrade: function (row) {
      this.list.forEach((v) => {
        v.selected = false
      })
      row.selected = true
      this.selid = row.id
    },
    submit: async function () {
      if (!this.selid) {
        C.error('请设置权限等级')
        return false
      }

      var data = {
        id: this.id,
        groupid: this.selid
      }
      let res = await C.ajax('/shop/rule/shopselectgrade', data, 'post')
      if (res) C.closeBrowserWindow(res)
    },
    time: function (unix) {
      let tm = new Date(unix * 1000)
      let yy = tm.getFullYear()
      let mm = tm.getMonth() < 9 ? '0' + (tm.getMonth() + 1) : tm.getMonth() + 1
      let dd = tm.getDate() < 10 ? '0' + tm.getDate() : tm.getDate()
      return yy + '-' + mm + '-' + dd
    },
    close: function () {
      C.closewin()
    },
    getList: async function () {
      let data = await C.ajax('/shop/rule/getgradeallot', {}, 'post')
      if (data) {
        data.forEach((v) => {
          if (this.selid == v.id) v.selected = true
          else v.selected = false
        })
        this.list = data
      }
    },
    selecRow: function (row, event, column) {
      this.list.forEach((row) => {
        row.selected = false
      })
      row.selected = true
      this.selid = row.id
    }
  },
  mounted: function () {
    let request = C.getDatas()
    this.selid = request.gid
    this.id = request.id
    this.getList()
  }
}
</script>